/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hotgammon.domain.board;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author truck
 */
public class StandardBoard implements Board {

    private HashMap<Location, Integer> locations;

    public StandardBoard() {
        locations = new HashMap<Location, Integer>();
    }

    /**
     * resets the two hashmaps and sets up the startconfiguration again
     */
    public void reset() {
        locations.clear();
        //RED IS NEGATIVE - BLACK IS POSITIVE
        locations.put(Location.R1, 2);
        locations.put(Location.R6, -5);
        locations.put(Location.R8, -3);
        locations.put(Location.R12, 5);
        locations.put(Location.B1, -2);
        locations.put(Location.B6, 5);
        locations.put(Location.B8, 3);
        locations.put(Location.B12, -5);
    }

    public void move(Location from, Location to) {
        // updates the number og bricks on each field
        Color fromColor = getColor(from);
        int fromNumber = getCount(from);
        int toNumber = getCount(to);
        
        locations.put(to, fromColor.getSign()*(toNumber+1));
        locations.put(from, fromColor.getSign()*(fromNumber-1));
    }

    public Color getColor(Location location) {
        if (locations.get(location) == null) {
            return Color.NONE;
        }
        return Color.getColorFromNumerical(locations.get(location));
    }

    public int getCount(Location location) {
        if (locations.get(location) == null) {
            return 0;
        }
        return Math.abs(locations.get(location));
    }

    public Iterator<Location> iterator() {
        return Arrays.asList(Location.values()).iterator();
    }
}


